import createMonitor, { ExceptionEvent, ExceptionHandler } from './monitor';
import { AbnormalData, AbnormalType } from './type';
import { formatMonitorId } from './util';
const monitorInterface = createMonitor();

const logInfo = (event: AbnormalType, data: AbnormalData) => {
    const { type, name, ...rest } = data || {};

    const trackingKey = formatMonitorId(event, type, name);
    monitorInterface.triggerHook(ExceptionEvent.Tracking, {
        monitorType: event,
        monitorSubType: type,
        monitorName: name,
        trackingKey,
        data: rest
    });
};
export const logHub = {
    api: (data: AbnormalData) => logInfo('api', data),
    script: (data: AbnormalData) => logInfo('script', data),
    network: (data: AbnormalData) => logInfo('network', data),
    websocket: (data: AbnormalData) => logInfo('websocket', data),
    logic: (data: AbnormalData) => logInfo('logic', data),
    resource: (data: AbnormalData) => logInfo('resource', data),
    interaction: (data: AbnormalData) => logInfo('interaction', data),
    storage: (data: AbnormalData) => logInfo('storage', data)
};

export const tracking = (key: string, data?: any) => {
    monitorInterface.triggerHook(ExceptionEvent.Tracking, {
        monitorType: 'tracking',
        trackingKey: key,
        data
    });
};

export const watchTracking = (callback: ExceptionHandler) => {
    monitorInterface.on(ExceptionEvent.Tracking, callback);
};

export const monitor = monitorInterface;

export type AbnormalDataType = AbnormalData;
export type AbnormalEventType = AbnormalType;
export { formatMonitorId } from './util';
